class MoveTo extends Action {
	constructor(enity, path, callback) {
		super();
		
		this.enity = enity;
		this.path = path;
        this.callback = callback;
	}
	
	update() {
		if(!this.path || this.path.length <= 0) {
			this.enity.action = null;
			this.enity.state = "idea";
			return;
		}
		
		// 计算距离
		const target = this.path[0];
		const speed = this.enity.speed;
		const xDiff = target.x - this.enity.centerPoint.x;
		const yDiff = target.y - this.enity.centerPoint.y;
		const distance = Math.sqrt(xDiff * xDiff + yDiff * yDiff);
		
		if(distance < speed/2) {
			this.enity.state = "idea";
			this.enity.centerPoint = this.path[0];
			this.path.splice(0,1);
            this.callback(this);
		}
        else {
            const hude = xDiff == 0 && yDiff == 0 ? 0 : Math.atan2(xDiff, yDiff);
                    
            // 计算位置
            const x = speed*Math.sin(hude);
            const y = speed*Math.cos(hude);

            if(this.enity.scene.battle)
                Matter.Body.setVelocity(this.enity.body, {x,y});
            else {
                this.enity.centerPoint = {x:this.enity.centerPoint.x+x,y:this.enity.centerPoint.y+y};
                updateEnityDirection(this.enity, hude);

                if(this.enity.state != "attack") 
                    this.enity.state = distance > 2 ? "run":distance < 0.08 ?'idea':"walk";
            }
        }
	}
}